package com.xcdh.ittools.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xcdh.commons.annotation.XCDHAspect;
import com.xcdh.commons.spring.aspect.XCDHHTTPRequestLog;
import com.api.ittools.entity.ITToolPage;
import com.xcdh.domain.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;
@Slf4j
public  class ITToolBaseController<T> {




    protected IService<T> service;

    public IService<T> getService() {
        return this.service;
    }

    @Autowired
    public void setService(IService<T> service){
        System.out.println("service====="+service);
        this.service = service ;
    }

    @PostMapping("/update")
    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    public R update(@RequestBody T data){
        return R.result(this.getService().updateById(data)) ;
    }
    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    @PostMapping("/updateBatch")
    public R updateBatch(List<T> data){
        return R.result(this.getService().updateBatchById(data)) ;
    }
    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    @PostMapping("/save")
    public R save(@RequestBody T data){
        return R.result(this.getService().save(data)) ;
    }

    @PostMapping("/saveBatch")
    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    public R saveBatch(@RequestBody List<T> data){
        return R.result(this.getService().saveBatch(data)) ;
    }

    @PostMapping("/deleteByIds")
    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    public R deleteById(@RequestBody List<? extends Serializable> ids){
        return R.result(this.getService().removeByIds(ids)) ;
    }
    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    @PostMapping("/listByPage")
    public R listByPage(@RequestBody ITToolPage<T> page){
        IPage<T> result = this.getService().page(page, page.getQuery());
        return R.okData(result);
    }

    @XCDHAspect(value=XCDHHTTPRequestLog.class)
    @PostMapping("/list")
    public R queryList(@RequestBody T data){
        Wrapper<T> queryWrapper = new QueryWrapper<>(data);
        List<T> list = this.getService().list(queryWrapper);
        return R.okData(list);
    }
}
